﻿var DOMAIN = "http://techmaster.dyndns.biz";

// Common Url
var GET_RANDOM_PHOTO_URL = DOMAIN + "/SocialPhotos/PhotoCloud.svc/getRandomPhotos?number=";
var GET_THUMNAIL_PHOTO_URL = DOMAIN + "/SocialPhotos/Thumbnail.ashx?f=";
var GET_IMAGE_URL = DOMAIN + "/SocialPhotos/Upload.ashx?f=";
var GET_USER_IMAGE_PHOTO = DOMAIN + "/SocialPhotos/PhotoCloud.svc/getPhotosByUserID?id=";
var GET_TOP_RATES_PHOTO = DOMAIN + "/SocialPhotos/PhotoCloud.svc/getTopRated?number=";
var GET_PHOTO_DETAIL = DOMAIN + "/SocialPhotos/PhotoCloud.svc/Photos(";
var LIKE = DOMAIN + "/SocialPhotos/PhotoCloud.svc/like?id=";
var DISLIKE = DOMAIN + "/SocialPhotos/PhotoCloud.svc/dislike?id=";
var GET_COMMENT = DOMAIN + "/SocialPhotos/PhotoCloud.svc/Comments?$filter=Photo/Id%20eq%20";
var POST_COMMENT = DOMAIN + "/SocialPhotos/Comment.ashx";

// SocialPhotos/PhotoCloud.svc/Comments?$filter=Photo/Id%20eq%2019&$orderby=PostedTime

var MAX_RANDOM_IMAGES = 6;
var MAX_PHOTOS = 9;

// Get Random Images
function GetRandomImages() {
    //    $("#images").empty().html('<img src="1-0.gif" />');
    var RequestUrl = GET_RANDOM_PHOTO_URL + MAX_RANDOM_IMAGES;
    var loading = "<li><img src=\"Images/loader.gif\" class=\"image0\"></li>";
    $("#ad-thumb-list").append(loading);
    $.getJSON(RequestUrl, {},
                                            function (data) {
                                                $("#ad-thumb-list").empty();
                                                $.each(data.d, function (i, item) {
                                                    var thumnail = GET_THUMNAIL_PHOTO_URL + item.FileName;
                                                    var image = GET_IMAGE_URL + item.FileName;
                                                    var str = "<li><a href=\"" + image + "\"><img src=\"" + thumnail + "\" class=\"image" + i + "\"></a></li>";
                                                    $("#ad-thumb-list").append(str);
                                                });

                                                // Initialize the slide
                                                InitializeSlide();
                                            });
}

function GetUserPhoto(userId) {
    $.getJSON(GET_USER_IMAGE_PHOTO + userId, {},
                                                    function (data) {
                                                        var str = "<div class=\"wrapper\">";
                                                        var j = 1;
                                                        $.each(data.d, function (i, item) {
                                                            var thumnail = GET_THUMNAIL_PHOTO_URL + item.FileName;
                                                            str += "<div class=\"left" + j + "\"><a href=\"/Photos/Photos/Detail/" + item.Id + "\"><img src=\"" + thumnail + "\" /></a><br/>Like: " + item.Likes + "/" + item.Dislikes + "</div>";
                                                            if (j >= 3) {
                                                                str += "</div><div class=\"wrapper\">";
                                                                j = 0;
                                                            }
                                                            j++;

                                                            if (i >= MAX_PHOTOS - 1) return false;
                                                        });

                                                        str += "</div>";
                                                        $("#myPhoto").append(str);

                                                    });
}

function GetTopRatedPhoto() {
    $.getJSON(GET_TOP_RATES_PHOTO + MAX_PHOTOS, {},
                                                    function (data) {
                                                        var str = "<div class=\"wrapper\">";
                                                        var j = 1;
                                                        $.each(data.d, function (i, item) {
                                                            var thumnail = GET_THUMNAIL_PHOTO_URL + item.FileName;
                                                            str += "<div class=\"left" + j + "\"><a href=\"/Photos/Photos/Detail/" + item.Id + "\"><img src=\"" + thumnail + "\" /></a><br/>Like: " + item.Likes + "/" + item.Dislikes + "</div>";
                                                            if (j >= 3) {
                                                                str += "</div><div class=\"wrapper\">";
                                                                j = 0;
                                                            }
                                                            j++;
                                                        });

                                                        str += "</div>";
                                                        $("#topRates").append(str);

                                                    });
}

function LoadPhotoDetail(photoId) {
    var ImageUrl = GET_PHOTO_DETAIL + photoId + ")";
    $("#Image").empty();
    $("<img/>").attr("src", loadingImage).appendTo("#Image");
    $("#like").empty();
    $("#dislike").empty();
    $("#title").empty();
    $.getJSON(ImageUrl, {},
                                                        function (data) {
                                                            $("#Image").empty();
                                                            var image = GET_IMAGE_URL + data.d.FileName;
                                                            $("<img/>").attr("src", image).appendTo("#Image");
                                                            $("#like").append("(" + data.d.Likes + ")");
                                                            $("#dislike").append("(" + data.d.Dislikes + ")");
                                                            $("#title").append("(" + data.d.Title + ")");
                                                        });
}

var loadingImage = "../../Images/loader.gif";
function LoadPhotoLike(photoId) {
    var ImageUrl = GET_PHOTO_DETAIL + photoId + ")";
    $("#like").empty();
    $("#dislike").empty();
    $("#title").empty();

    $.getJSON(ImageUrl, {},
                                                        function (data) {
                                                            $("#like").append("(" + data.d.Likes + ")");
                                                            $("#dislike").append("(" + data.d.Dislikes + ")");
                                                            $("#title").append("(" + data.d.Title + ")");
                                                        });
}

function like(imageId) {
                                                var likeUrl = LIKE + imageId;
                                                var oldImage = $("#likeImage").attr("src");
                                                $("#likeImage").empty().attr("src", loadingImage);
                                                        $.getJSON(likeUrl, {},
                                                        function (data) {
                                                            $("#likeImage").empty().attr("src", oldImage);
                                                            LoadPhotoLike(imageId);
                                                        });
}

function dislike(imageId) {
    var likeUrl = DISLIKE + imageId;
    var oldImage = $("#dislikeImage").attr("src");
    $("#dislikeImage").empty().attr("src", loadingImage);
    $.getJSON(likeUrl, {},
                                                        function (data) {
                                                            $("#dislikeImage").empty().attr("src", oldImage);
                                                            LoadPhotoLike(imageId);
                                                        });
}

function GetComment(imageId) {
    var commentUrl = GET_COMMENT + imageId + "&$orderby=PostedTime&20%asc";
    $("#comment").empty();
    $("<li/>").html("<img src=\"" + loadingImage + "\"/>").appendTo("#comment");
    $.getJSON(commentUrl, {},
                                                 function (data) {
                                                            var str = "";
                                                            $.each(data.d, function (i, item) {
                                                                str += "<li>\"" + item.Content + "\"</li>";
                                                            });

                                                            $("#comment").empty();
                                                            $("#comment").append(str);
    });
}

function PostCommentComplete(response) {
    $("#comment-post").html("");
    GetComment($("#photoId").val());
    $("#submit_loading_image").empty()
}

function submitForm() {
    $("#submit_loading_image").empty();

    if ($("#comment-post").html() == "") {
        $("#comment-border").css("border-color", "red");
        return false;
    }
    $("#comment-border").css("border-color", "#CCCCCC");
    $("<img/>").attr("src", loadingImage).appendTo("#submit_loading_image");
    $("#post-comment").submit();
}




